/* eslint-disable no-use-before-define */
/* eslint-disable max-len */
import { SetupContext } from 'vue';
import { DgControl } from '../../../designer-canvas/src/composition/dg-control';
import { ElementPropertyConfig } from '../../../designer-canvas/src/composition/entity/property-entity';
import { numberSpinnerProps, NumberSpinnerProps } from '../number-spinner.props';
import { ComponentSchema } from "../../../designer-canvas/src/types";
import { DesignerHTMLElement, DraggingResolveContext, UseDesignerRules } from "../../../designer-canvas/src/composition/types";

export function useDesignerRules(schema: any, parentSchema: any) {
    const canAccept = true;
    let propertyConfig = [];
    function canAccepts() {
        return canAccept;
    }
    function getBasicPropConfig(schema: any, viewModelId?: string): ElementPropertyConfig {

        return {
            categoryId: 'basic',
            categoryName: '基本信息',
            properties: [
                {
                    propertyID: 'id',
                    propertyName: '标识',
                    propertyType: 'string',
                    description: '组件的id',
                    readonly: true
                },
                {
                    propertyID: 'type',
                    propertyName: '控件类型',
                    propertyType: 'select',
                    description: '组件的类型',
                    iterator: [{ key: schema.type, value: DgControl.Splitter.name }],
                }
            ],
        };
    }

    function getAppearanceProperties(propertyData: any): ElementPropertyConfig {
        return {
            categoryId: 'appearance',
            categoryName: '外观',
            properties: [
                {
                    propertyID: 'viewType',
                    propertyName: '默认视图类型',
                    propertyType: 'select',
                    description: '组件默认显示的视图类型',
                    iterator: [{ key: 'day', value: '日视图' }, { key: 'week', value: '周视图' }]
                },
                {
                    propertyID: 'placeNameWidth',
                    propertyName: '地点列宽度',
                    propertyType: 'number',
                    decimals: 0,
                    min: 150
                },
                {
                    propertyID: 'rowHeight',
                    propertyName: '日视图行高',
                    propertyType: 'number',
                    decimals: 0,
                    min: 62
                },
                {
                    propertyID: 'weekRowHeight',
                    propertyName: '周视图行高',
                    propertyType: 'number',
                    decimals: 0,
                    min: 85
                },
                {
                    propertyID: 'weekCellWidth',
                    propertyName: '周视图列宽度',
                    propertyType: 'number',
                    decimals: 0,
                    min: 150
                },
            ]
        };
    }

    function getBehaviorProperties(propertyData: any, viewModelId: string) {
        // const visibleProp = getVisiblePropertyEntity(propertyData, viewModelId);

        return {
            categoryId: 'behavior',
            categoryName: '行为',
            properties: [
                // visibleProp
            ]
        };
    }
    function getDayViewPropConfig(propertyData: any, viewModelId: string): ElementPropertyConfig {
        return {
            categoryId: 'dayView',
            categoryName: '日视图配置',
            properties: [
                {
                    propertyID: 'contentTemplate',
                    propertyName: '日视图预定信息模板',
                    propertyType: 'modal',
                    // editor: TemplateBindingEditorComponent,
                    editorParams: {
                        listViewId: propertyData.id,
                        templateUri: 'assets/form/preset-template/appointment-calendar-template.json',
                        schemaFields: [],
                        // viewModelId: viewModelId
                    },
                    // beforeOpenModal(): BeforeOpenModalResult {
                    //     // 取字段最新值
                    //     editorParams.schemaFields = self.getPanelMappingFields();
                    //     return { result: true, message: '' };
                    // }
                },
                {
                    propertyID: 'dayViewCls',
                    propertyName: '日视图自定义样式',
                    propertyType: 'modal',
                    // editor: CodeEditorComponent,
                    // editorParams: {
                    //     language: 'javascript',
                    //     exampleCode: '(item) => {\r\n    const data = item.data;\r\n    return {\r\n        gray: data.level === \'1\',\r\n        blue: data.level === \'2\',\r\n        ' +
                    //         'pink: data.level === \'3\',\r\n        red: data.level === \'4\'\r\n    };\r\n}\r\n\r\n注：根据预定类型返回不同的样式，其中level为预定类型字段在数据库中的字段编号。'
                    // }
                },
                {
                    propertyID: 'reservationTitle',
                    propertyName: '新增预定文本',
                    propertyType: 'string',
                    description: '鼠标滑过日视图空白单元格时显示的文本'
                }
            ]
        };
    }
    function getPlacePropConfig(propertyData: any, viewModelId: string): ElementPropertyConfig {
        // const viewModel = self.domService.getViewModelById(viewModelId);

        return {
            categoryId: 'place',
            categoryName: '地点配置',
            properties: [
                {
                    propertyID: 'placeTitle',
                    propertyName: '地点列标题',
                    propertyType: 'string',
                    description: '地点的展示文本'
                },
                {
                    propertyID: 'placeDataSource',
                    propertyName: '地点数据源',
                    propertyType: 'modal',
                    description: '选择地点数据源视图对象',
                    // editor: PlaceDataSourceSelectorComponent,
                    editorParams: propertyData,
                    // converter: new PlaceDataSourceConverter()
                },
                {
                    propertyID: 'placeDataSourceUrl',
                    propertyName: '地点数据源请求url',
                    propertyType: 'string',
                    description: '获取地点列表的API地址',
                    readonly: true,
                    visible: propertyData.placeDataSource
                },
                {
                    propertyID: 'placeDataSourceUrlType',
                    propertyName: '地点数据源请求类型',
                    propertyType: 'select',
                    description: '地点数据源请求类型',
                    iterator: [{ key: 'PUT', value: 'PUT' }, { key: 'POST', value: 'POST' }, { key: 'GET', value: 'GET' }],
                    readonly: true,
                    visible: propertyData.placeDataSource
                },
                {
                    propertyID: 'placeNameTemplate',
                    propertyName: '地点展示模板',
                    propertyType: 'modal',
                    // editor: PlaceTemplateEditorComponent,
                    editorParams: propertyData,
                    // beforeOpenModal(): BeforeOpenModalResult {
                    //     if (!propertyData.placeDataSource) {
                    //         return { result: false, message: '请先选择地点数据源' };
                    //     }
                    //     editorParams = propertyData;
                    //     return { result: true, message: '' };
                    // },
                    visible: propertyData.placeDataSource

                },
                {
                    propertyID: 'placeIdField',
                    propertyName: '地点标识字段',
                    propertyType: 'modal',
                    // editor: PlaceFieldSelectorComponent,
                    editorParams: {
                        voId: propertyData.placeDataSource && propertyData.placeDataSource.voId
                    },
                    // beforeOpenModal(): BeforeOpenModalResult {
                    //     if (!propertyData.placeDataSource) {
                    //         return { result: false, message: '请先选择地点数据源' };
                    //     }
                    //     editorParams.voId = propertyData.placeDataSource && propertyData.placeDataSource.voId;
                    //     return { result: true, message: '' };
                    // },
                    visible: propertyData.placeDataSource
                },
                {
                    propertyID: 'placeNameField',
                    propertyName: '地点名称字段',
                    propertyType: 'modal',
                    // editor: PlaceFieldSelectorComponent,
                    editorParams: {
                        voId: propertyData.placeDataSource && propertyData.placeDataSource.voId
                    },
                    // beforeOpenModal(): BeforeOpenModalResult {
                    //     if (!propertyData.placeDataSource) {
                    //         return { result: false, message: '请先选择地点数据源' };
                    //     }
                    //     editorParams.voId = propertyData.placeDataSource && propertyData.placeDataSource.voId;
                    //     return { result: true, message: '' };
                    // },
                    visible: propertyData.placeDataSource
                }
            ],
            setPropertyRelates(changeObject, data, parameters?) {
                switch (changeObject.propertyID) {
                // case 'placeDataSource': {
                //     properties.forEach((prop: any) => {
                //         if (prop.propertyID !== 'placeTitle' && prop.propertyID !== 'placeDataSource') {
                //             prop.visible = !!changeObject.propertyValue;
                //         }
                //     });
                //     break;
                // }

                case 'placeIdField': case 'placeNameField': {
                    // 修改地点标识、名称列后需要同步命令中的地点参数
                    const placeIdParam = propertyData.placeIdField;
                    const placeNameParam = propertyData.placeNameField;
                    // const addNode = viewModel.commands.find((item: any) => item.handlerName === 'OpenAppAndAdd' && (item.cmpId === '70b4abd4-9f2c-4b7c-90e9-6ac6f4b74c72'));
                    // if (addNode && addNode.params) {
                    //     addNode.params.find((item: any) => item.name === 'params').value =
                    //             "{\"action\":\"LoadAndAdd1\",\"start\":\"{EVENTPARAM~/start}\",\"end\":\"{EVENTPARAM~/end}\",\"placeId\":\"{EVENTPARAM~/place/" + placeIdParam + "}\",\"placeName\":\"{EVENTPARAM~/place/" + placeNameParam + "}\"}";
                    // }
                    break;
                }
                }
            },
        };
    }

    function getPropConfig(schema: ComponentSchema): ElementPropertyConfig[] {
        propertyConfig = [];

        // 基本信息属性
        const basicPropConfig = getBasicPropConfig(schema);
        propertyConfig.push(basicPropConfig);

        // // 样式属性
        // const appearanceStylePropConfig = ControlAppearancePropertyConfig.getAppearanceStylePropConfigs(propertyData);
        // propertyConfig = propertyConfig.concat(appearanceStylePropConfig);

        // // 外观属性
        // const appearancePropConfig = getAppearanceProperties(propertyData);
        // propertyConfig.push(appearancePropConfig);

        // // 行为属性
        // const behaviorPropConfig = getBehaviorProperties(propertyData, viewModelId);
        // propertyConfig.push(behaviorPropConfig);

        // // 地点属性
        // const placePropConfig = getPlacePropConfig(propertyData, viewModelId);
        // propertyConfig.push(placePropConfig);

        // // 日视图属性
        // const dayViewPropConfig = getDayViewPropConfig(propertyData, viewModelId);
        // propertyConfig.push(dayViewPropConfig);

        return propertyConfig;
    }

    return {
        canAccepts,
        getPropConfig
    } as UseDesignerRules;
}
